// ignore_for_file: prefer_const_constructors, prefer_const_literals_to_create_immutables
import 'package:flutter/material.dart';

import 'package:permission_handler/permission_handler.dart';
import 'package:junyeyishu/widgets/QGWidget.dart';
import 'package:junyeyishu/utils/utils.dart';
import 'package:junyeyishu/widgets/QGButton.dart';

class QGOpenWalletPage extends StatefulWidget {
  const QGOpenWalletPage({super.key});
  @override
  State<QGOpenWalletPage> createState() => _QGOpenWalletPageState();
}

class _QGOpenWalletPageState extends State<QGOpenWalletPage> {
  final nameController = TextEditingController();
  final identityController = TextEditingController();

  @override
  void dispose() {
    nameController.dispose();
    identityController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return QGScaffold(
        bgColor: Color(0xFFF8F8F6),
        title: '开通汇付支付',
        body: GestureDetector(
          onTap: () {
            unfocus(context);
          },
          child: Column(
            children: [
              buildInfo(),
              SizedBox(height: 30.rpx),
              Container(
                margin: EdgeInsets.symmetric(horizontal: 20.rpx),
                child: QGButton(title: "确定", onTap: onCommit),
              ),
            ],
          ),
        ));
  }

  buildInfo() {
    return Container(
      margin: EdgeInsets.fromLTRB(10.rpx, 15.rpx, 10.rpx, 0),
      padding: EdgeInsets.symmetric(horizontal: 15.rpx),
      decoration: BoxDecoration(
        borderRadius: BorderRadius.circular(10.rpx),
        color: Color(0xFFFFFFFF),
      ),
      child: Column(
        children: [
          buildName(),
          buildIdentity(),
        ],
      ),
    );
  }

  buildName() {
    return Row(
      children: [
        Expanded(
            child: Container(
          height: 55.rpx,
          decoration: BoxDecoration(
              border: Border(
                  bottom:
                      BorderSide(width: 0.5.rpx, color: Color(0xFFEAEAEA)))),
          child: Row(children: [
            buildTitle('姓名', 14, FontWeight.bold),
            SizedBox(
              width: 10.rpx,
            ),
            Expanded(child: buildInput(nameController, '请输入真实姓名'))
          ]),
        ))
      ],
    );
  }

  buildIdentity() {
    return Row(children: [
      Expanded(
          child: Container(
        height: 55.rpx,
        decoration: BoxDecoration(
            border: Border(
                bottom: BorderSide(width: 0.5.rpx, color: Color(0xFFEAEAEA)))),
        child: Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            buildTitle('身份证号', 14, FontWeight.bold),
            SizedBox(
              width: 10.rpx,
            ),
            Expanded(child: buildInput(identityController, '请输入身份证号')),
          ],
        ),
      ))
    ]);
  }

  buildTitle(String name, int size, FontWeight weight) {
    return Text(
      name,
      style: TextStyle(
          fontSize: size.rpx, fontWeight: weight, color: Color(0xFF111111)),
    );
  }

  buildInput(controller, text) {
    return TextField(
      controller: controller,
      keyboardType: TextInputType.text,
      cursorColor: Color(0xFF111111),
      textAlign: TextAlign.end,
      style: TextStyle(color: Color(0xFF111111)),
      decoration: InputDecoration(
          hintText: text,
          hintStyle: TextStyle(color: Color(0xFF999999)),
          border: OutlineInputBorder(borderSide: BorderSide.none),
          contentPadding: EdgeInsets.zero,
          enabledBorder: InputBorder.none,
          focusedBorder: InputBorder.none),
      onChanged: (value) {
        // checkInput();
      },
    );
  }

  Future<bool> checkPermission() async {
    // var status = await Permission.camera.request();
    // if (status.isDenied) {
    //   toast("请允许app使用相机");
    //   return false;
    // }

    await Permission.camera.request();
    await Permission.microphone.request();
    await Permission.photos.request();

    // status = await Permission.photosAddOnly.request();
    // if (status.isDenied) {
    //   toast("请允许app访问相册");
    //   return false;
    // }
    return true;
  }

  onCommit() async {
    unfocus(context);

    if (nameController.text.isEmpty) {
      toast("请输入姓名");
      return;
    }
    if (identityController.text.isEmpty) {
      toast("请输入身份证号码");
      return;
    }

    var ctx = context;

    var param = {};
    param['user_name'] = nameController.text;
    param['id_card'] = identityController.text;
Map<String, dynamic> stringMap = param.cast<String, dynamic>();
    Network.post(url_hf_openWallet, data: stringMap, context: context)
        .then((res) async {
      if (!res.success) {
        toast(res.msg);
        return;
      }

      // toBrowser(res.data["url"] ?? "");

      checkPermission();
      Navigator.of(context).pop();
      Navigator.of(context).pushReplacementNamed(PageQGPayWebView,
          arguments: res.data["redirect_url"] ?? "");
    });
  }

  // toBrowser(url) async {
  //   await launchUrl(Uri.parse(url), mode: LaunchMode.externalApplication);
  //   Navigator.of(context).pop();
  // }
}
